﻿/*
 * Erstellt mit SharpDevelop.
 * Benutzer: Kai Patrick
 * Datum: 17.03.2008
 * Zeit: 19:40
 * 
 * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;

namespace Gamadus.Designer
{
	/// <summary>
	/// Description of EditSpriteForm.
	/// </summary>
	public partial class EditRoomForm : Form, IEditWindow
	{
		Dictionary<MenuItem, Object> chooseObjectList = new Dictionary<MenuItem, Object>();
		List<Instance> instanceList = new List<Instance>();
		Room res;
		
		Gamadus.Designer.Object chosenObject;
		Gamadus.Designer.Object ChosenObject
		{
			get
			{
				return chosenObject;
			}
			set
			{
				chosenObject = value;
				if(chosenObject == null)
					labelObjectChoose.Text = "Please choose";
				else
					labelObjectChoose.Text = chosenObject.Name;
			}
		}
		int snapX;
		int snapY;
		
		int SnapX
		{
			get
			{
				return snapX;
			}
			set
			{
				snapX = value;
				pictureBoxRoom.Refresh();
			}
		}
		
		int SnapY
		{
			get
			{
				return snapY;
			}
			set
			{
				snapY = value;
				pictureBoxRoom.Refresh();
			}
		}
		
		public EditRoomForm(Room res)
		{
			this.res = res;
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			this.MdiParent = ResourceMgr.Instance.MainForm;
			this.Show();
			
			this.UpdateEditForm();
			
			textBoxName.Validating += ResourceMgr.Instance.ValidateResourceNameText;
			textBoxSpeed.Validating += ResourceMgr.Instance.ValidatePositiveIntegerText;
			textBoxWidth.Validating += ResourceMgr.Instance.ValidatePositiveIntegerText;
			textBoxHeight.Validating += ResourceMgr.Instance.ValidatePositiveIntegerText;
			toolStripTextBoxSnapX.Validating += ResourceMgr.Instance.ValidatePositiveIntegerText;
			toolStripTextBoxSnapY.Validating += ResourceMgr.Instance.ValidatePositiveIntegerText;
			
			instanceList = res.CloneInstanceList();
			pictureBoxRoom.BackColor = (Color)res.Settings["bgcolor"];
			labelBackColorChoose.BackColor = pictureBoxRoom.BackColor;
			
			textBoxCaption.Text = (string)res.Settings["caption"];
			textBoxSpeed.Text = ((int)res.Settings["speed"]).ToString();
			textBoxWidth.Text = ((int)res.Settings["width"]).ToString();
			textBoxHeight.Text = ((int)res.Settings["height"]).ToString();
			snapX = (int)res.Settings["snapx"];
			snapY = (int)res.Settings["snapy"];
			toolStripTextBoxSnapX.Text = snapX.ToString();
			toolStripTextBoxSnapY.Text = snapY.ToString();
			
			ChosenObject = (Object)ResourceMgr.Instance.GetFirst(ResourceType.Object);
			
		}
		
		public Resource Editing 
		{
			get
			{
				return res;
			}
		}
		
		public void UpdateEditForm()
		{
			textBoxName.Text = res.Name;
		}
		
		bool closeDontAsk = false;
		
		public void Close(bool askForSave)
		{
			closeDontAsk = !askForSave;
			this.Close();
		}
		
		void EditSpriteFormFormClosing(object sender, FormClosingEventArgs e)
		{
			//check for unvalid text in validated textboxes (FIX for bug #3)
			if(!ResourceMgr.Instance.IsValidName(textBoxName.Text, res)  || 
			   !ResourceMgr.Instance.IsValidPositiveInteger(textBoxSpeed.Text) ||
			   !ResourceMgr.Instance.IsValidPositiveInteger(textBoxWidth.Text) ||
			   !ResourceMgr.Instance.IsValidPositiveInteger(textBoxHeight.Text) ||
			   !ResourceMgr.Instance.IsValidPositiveInteger(toolStripTextBoxSnapX.Text) ||
			   !ResourceMgr.Instance.IsValidPositiveInteger(toolStripTextBoxSnapY.Text))
			{
				e.Cancel = true;
				return;
			}
			
			bool save = true;
			if(!closeDontAsk)
			{
				DialogResult result = MessageBox.Show("Save changes to "+res.Name+"?","Warning",
				                                      MessageBoxButtons.YesNoCancel,
				                                      MessageBoxIcon.Warning);
				if(result == DialogResult.Cancel)
				{
					e.Cancel = true;
					return;
				}
				if(result == DialogResult.No)
					save = false;
			}
			closeDontAsk = false;
			
			if(save)
			{
				res.Settings["caption"] = textBoxCaption.Text;
				res.Settings["speed"] = int.Parse(textBoxSpeed.Text);
				res.Settings["width"] = int.Parse(textBoxWidth.Text);
				res.Settings["height"] = int.Parse(textBoxHeight.Text);
				res.Settings["bgcolor"] = pictureBoxRoom.BackColor;
				res.Settings["snapx"] = int.Parse(toolStripTextBoxSnapX.Text);
				res.Settings["snapy"] = int.Parse(toolStripTextBoxSnapY.Text);
				res.WriteInstanceList(instanceList);
				
				res.Name = textBoxName.Text;
			}
			res.CloseEditorHandle(save);
		}
		
		void ButtonCreationCodeClick(object sender, EventArgs e)
		{
			throw new NotImplementedException();
		}
		
		void ToolStripButtonUndoClick(object sender, EventArgs e)
		{
			throw new NotImplementedException();
		}
		
		void ToolStripButtonOKClick(object sender, EventArgs e)
		{
			this.Close(false);
		}
		
		void PictureBoxRoomPaint(object sender, PaintEventArgs e)
		{
			Graphics g = e.Graphics;
			Pen linePen = new Pen(Color.White);
			for(int x = 0; x < pictureBoxRoom.Size.Width; x += SnapX)
			{
				g.DrawLine(linePen, x,0,x,pictureBoxRoom.Size.Height);
			}
			for(int y = 0; y < pictureBoxRoom.Size.Height; y += SnapY)
			{
				g.DrawLine(linePen, 0, y, pictureBoxRoom.Size.Width, y);
			}
			linePen.Dispose();
			
			Image image = imageListIcons.Images["unknown-sprite"];
			foreach(Instance inst in instanceList)
			{
				g.DrawImage(image,inst.Position);
			}
			
		}
		
		void TextBoxWidthTextChanged(object sender, EventArgs e)
		{
			if(!ResourceMgr.Instance.IsValidPositiveInteger(textBoxWidth.Text))
				return;
		
			pictureBoxRoom.Size = new Size(int.Parse(textBoxWidth.Text),pictureBoxRoom.Size.Height);
		}
		
		void TextBoxHeightTextChanged(object sender, EventArgs e)
		{
			if(!ResourceMgr.Instance.IsValidPositiveInteger(textBoxHeight.Text))
				return;
		
			pictureBoxRoom.Size = new Size(pictureBoxRoom.Size.Width, int.Parse(textBoxHeight.Text));
		}
		
		
		void ToolStripTextBoxSnapXTextChanged(object sender, EventArgs e)
		{
			if(!ResourceMgr.Instance.IsValidPositiveInteger(toolStripTextBoxSnapX.Text))
				return;
			
			SnapX = int.Parse(toolStripTextBoxSnapX.Text);
		}
		
		void ToolStripTextBoxSnapYTextChanged(object sender, EventArgs e)
		{
			if(!ResourceMgr.Instance.IsValidPositiveInteger(toolStripTextBoxSnapY.Text))
				return;
			
			SnapY = int.Parse(toolStripTextBoxSnapY.Text);
		}
		
		void LabelBackColorChooseClick(object sender, EventArgs e)
		{
			ColorDialog dialog = new ColorDialog();
			dialog.Color = pictureBoxRoom.BackColor;
			dialog.FullOpen = true;
			
			if(dialog.ShowDialog() != DialogResult.OK)
				return;
			
			pictureBoxRoom.BackColor = dialog.Color;
			labelBackColorChoose.BackColor = dialog.Color;
		}
		
		void PictureBoxRoomMouseMove(object sender, MouseEventArgs e)
		{
			toolStripStatusLabel.Text = "x: "+e.X+" - y: "+e.Y;
		}
		
		void ObjectChooseClick(object sender, EventArgs e)
		{
			ChosenObject = chooseObjectList[sender as MenuItem];
			chooseObjectList.Clear();
		}
		
		void LabelObjectChooseClick(object sender, EventArgs e)
		{
			//TODO: handle subfolders
			ContextMenu menu = new ContextMenu();
			foreach(TreeNode node in ResourceMgr.Instance.TypeNodes[ResourceType.Object].Nodes)
			{
				if(ResourceMgr.Instance.Resources.ContainsKey(node))
				{
					Gamadus.Designer.Object obj = (Object)ResourceMgr.Instance.Resources[node];
					MenuItem item = new MenuItem(obj.Name, ObjectChooseClick);
					menu.MenuItems.Add(item);
					chooseObjectList[item] = obj;
				}
			}
			if(menu.MenuItems.Count > 0)
				menu.Show(labelObjectChoose, new Point(0,0));
			
		}
		
		void PictureBoxRoomMouseClick(object sender, MouseEventArgs e)
		{
			if(chosenObject == null)
				return;
			
			instanceList.Add(new Instance(ChosenObject, e.Location));
			pictureBoxRoom.Refresh();
		}
	}
}
